package com.atguigu.gulimall.order.service.impl;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.service.OrderItemService;

@RabbitListener(queues = {"hellow-java-Queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * queues 声明要监听的队列
     * 返回值为org.springframework.amqp.core.Message
     * Message message原生消息类型
     * T<> 接收数据的数据类型
     * Channel channel 当前传输数据的通道
     */
    // 消息只能被一个客户端接受 业务处理期间不会接受其他消息
    //@RabbitListener(queues = {"hellow-java-Queue"})
    @RabbitHandler
    public void recieveMessage(Message message, OrderReturnReasonEntity content, Channel channel) {
        System.out.println("消息接受");
        // 消息体
        byte[] body = message.getBody();
        // 消息头属性信息
        MessageProperties messageProperties = message.getMessageProperties();
        // 消息的自增唯一ID
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println(deliveryTag);
        /*
        消费端确认 默认自动确认 只要接收到消息 服务端自动移除
                        channel.basicAck(deliveryTag, false);
                        拒绝策略
                        channel.basicNack(deliveryTag, false, false);
        */
        // 确认签收 非批量签收
        try {
            if (deliveryTag % 2 == 0) {
                channel.basicAck(deliveryTag, false);
                System.out.println("消息确认" + deliveryTag);
            } else {
                //    long var1 消息ID, boolean var3 批量删除, boolean var 重新入队
                channel.basicNack(deliveryTag, false, false);
                //    long var1, boolean var3
                //    channel.basicReject();
            }
        } catch (IOException e) {
            // 网络异常
        }
    }

    //@RabbitListener(queues = {"hellow-java-Queue"})
    @RabbitHandler
    public void recieveMessage2(OrderEntity content, Channel channel) {
        // 消息体
        System.out.println("接收到消息" + content);
    }
}